package com.zb.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zb.Service.UserService;
import com.zb.Service.impl.UserServiceImpl;
import com.zb.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/loginUserServlet")
public class LoginUserServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");

        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");

        System.out.println(userName);
        System.out.println(passWord);

        User user = userService.loginUser(userName, passWord);

        System.out.println(user);

        if(user!=null){
            //登录成功
            response.getWriter().write("success");
        }else {
            // 登陆失败
            response.getWriter().write("error");
        }

//        BufferedReader br = request.getReader();
//        String params = br.readLine();
//
//        User user = JSON.parseObject(params, User.class);
//        String userName = user.getUserName();
//        String passWord = user.getPassWord();
//        User u = userService.loginUser(userName, passWord);

//        if(u!=null){
//            //登录成功
//            response.getWriter().write("success");
//        }else {
//            // 登陆失败
//            response.getWriter().write("error");
//        }
    }
}
